const crypto = require('crypto');
module.exports.getRunParams = () => {
    let PORT = 3636, DEBUG = false, RUN = '', SECRET = '';
    let argv = process.argv.slice(2);
    //console.log(argv)
    if (argv.includes('--debug')){
        DEBUG = true;
    }
	if (argv.includes('--port')){
        let port = Number(argv[argv.indexOf('--port') + 1]);
        if (!! port) PORT = port;
    }

    if (argv.includes('-p')){
        let port = Number(argv[argv.indexOf('-p') + 1]);
        if (!! port) PORT = port;
    }
    if (argv.includes('-r')){
        RUN = argv[argv.indexOf('-r') + 1];
    }
    if (argv.includes('-s')){
        SECRET = argv[argv.indexOf('-s') + 1];
    }
    return {PORT, DEBUG, RUN, SECRET};
}

module.exports.encode = (string, secret) => {
    if (! string || ! secret) return string;
    let cipher = crypto.createCipher('aes256', secret);
    let str = cipher.update(string, 'utf8', 'hex');
    str += cipher.final('hex');
    return str;
}

module.exports.decode = (string, secret) => {
    if (! string || ! secret) return string;
    let decipher = crypto.createDecipher('aes256', secret);
    let str = decipher.update(string, 'hex', 'utf8');
    str += decipher.final('utf8');
    return str;
}
